﻿
var log = require("./../../util/loginfo").getInstand;

//房间ID
var rooms = {};
//用户列表
var userLocation = {};
//总放房间数
var totalRooms = 0;
//定时器集合
var timerList = {};

exports.destroy = function(roomId) {
    var roomInfo = rooms[roomId];
    if (roomInfo == null) {
        return;
    }
    delete rooms[roomId];
    totalRooms--;
}
exports.getTotalRooms = function() {
    return totalRooms;
}
exports.getRoom = function(roomId) {
    return rooms[roomId];
};
exports.addOnlineCount = function(roomId) {
    if (!rooms[roomId]) {
        return;
    }
    rooms[roomId].onlineCount++;
    if (timerList[roomId] && timerList[roomId] != null) {
        clearTimeout(timerList[roomId]);
        timerList[roomId] = null;
    }
};

exports.isCreator = function(roomId, userId) {
    var roomInfo = rooms[roomId];
    if (roomInfo == null) {
        return false;
    }
    return roomInfo.conf.creator == userId;
};

exports.createRoom = function(roomId, roomInfo) {
    rooms[roomId] = roomInfo;
    totalRooms++;
};

exports.enterRoom = function(roomId, userData, callback) {
    var userId = userData.userId;
    var fnTakeSeat = function(room) {
        if (exports.getUserRoom(userId) == roomId) {
            //已存在
            return 0;
        }
        var seat = room.seats[userData.seatId];
        if(seat.userId <= 0)
        {
            seat.userId = userData.userId;
            seat.name = userData.nickname;
            seat.headimgurl = userData.headimgurl;
            if(userData.score> userData.init_score){
                seat.score = userData.init_score;
            }else {
                seat.score = userData.score;
            }
            seat.robot = userData.userType;
            userLocation[userData.userId] = {
                roomId: roomId,
                seatIndex: userData.seatId
            };
            //加入等待对列
            var waitDict = {};
            waitDict.userid = userData.userId;
            waitDict.seatIndex = userData.seatId;
            //这个地方需要初始化一些新加入的用户信息
            waitDict.money = seat.score;
            //TODO 判断玩家的资格,玩家是否能加入游戏
            //**暂时未完成**
            //新加的玩家等待下一局游戏
            if (!room.waitingUsers) {
                room.waitingUsers = [];
            }
            room.waitingUsers.push(waitDict);
            log.info("座位信息:" + JSON.stringify(seat));
            //正常
            return 0;
        }
        //桌子满了
        return 1;
    }
    var room = rooms[roomId];
    if (room) {
        var ret = fnTakeSeat(room);
        callback(ret);
    } else {
        callback(2);
    }
};
exports.setReady = function(userId, value) {
    var roomId = exports.getUserRoom(userId);
    if (roomId == null) {
        return;
    }
    var room = exports.getRoom(roomId);
    if (room == null) {
        return;
    }
    var seatIndex = exports.getUserSeat(userId);
    if (seatIndex == null) {
        return;
    }
    var s = room.seats[seatIndex];
    s.ready = value;
}
exports.isReady = function(userId) {
    var roomId = exports.getUserRoom(userId);
    if (roomId == null) {
        return;
    }
    var room = exports.getRoom(roomId);
    if (room == null) {
        return;
    }
    var seatIndex = exports.getUserSeat(userId);
    if (seatIndex == null) {
        return;
    }
    var s = room.seats[seatIndex];
    return s.ready;
}
exports.getUserRoom = function(userId) {
    var location = userLocation[userId];
    if (location != null) {
        return location.roomId;
    }
    return null;
};
exports.getUserSeat = function(userId) {
    var location = userLocation[userId];
    if (location != null) {
        return location.seatIndex;
    }
    return null;
};
exports.getUserLocations = function() {
    return userLocation;
};
exports.exitRoom = function(userId) {
    log.info("用户 :"  + userId  + "离开房间!");
    var location = userLocation[userId];
    if (location == null) return;
    var roomId = location.roomId;
    var seatIndex = location.seatIndex;
    var room = rooms[roomId];
    delete userLocation[userId];
    if (room == null || seatIndex == null) {
        return;
    }
    var seat = room.seats[seatIndex];
    seat.userId = 0;
    seat.score = 0;
    seat.name = "";
    seat.ready = false;
    seat.headimgurl = "";
};